<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active {
      background-color: rgb(204, 145, 36);
    }
  </style>
</head>

<body>
  <div id="app">
    <h2>开始学习路由</h2>
    <router-link tag="button" to="/home">首页</router-link>
    <router-link tag="button" to="/news">新闻</router-link>
    <router-link tag="button" to="/about">关于我们</router-link>
    <hr>
    <!-- 视图上定义一个 出口 路由组件渲染的地方 -->
    <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 定义好三个组件 参数对象
    const Home = {
      template: `
        <div>
          <h1>我是首页</h1>  
        </div>
      `
    }
    const News = {
      template: `
        <div>
          <h1>我是新闻页</h1>  
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h1>我是关于我们页</h1>  
        </div>
      `
    }

    const NotFound = {
      template: `
        <div>
          <h1>我是404页</h1>  
        </div>
      `
    }
    const Login = {
      template: `
        <div>
          <h1>我是登录页</h1>  
          <button @click="doLogin">登录</button>
        </div>
      `,
      methods: {
        doLogin() {
          setTimeout(() => {
            const token = 'dwhifdhegrgdxef345t';
            // 立即缓存起来 跳转到 
            localStorage.setItem('token', token);
            if (this.$route.query.from) {
              this.$router.replace(this.$route.query.from)
            } else {
              this.$router.replace('/home')
            }

          }, 1000)
        }
      }
    }

    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        name: 'home',
        meta: {
          needLogin: false
        },
        component: Home
      },
      {
        path: '/news',
        name: "news",
        meta: {
          needLogin: false
        },
        component: News
      },
      {
        path: '/about',
        name: 'about',
        meta: {
          needLogin: true
        },
        component: About
      },
      {
        path: '/login',
        name: 'login',
        meta: {
          needLogin: false
        },
        component: Login
      },
      {
        path: '*',
        component: NotFound
      }
    ]
    // 创建路由对象实例
    const router = new VueRouter({
      routes
    })
    // 登录鉴权
    router.beforeEach((to, from, next) => {
      /* 
        判断是否需要登录
          需要 
            判断是否登录
          不需要
            直接放行
      */
      if (to.meta.needLogin) {
        //  需要登录
        const token = localStorage.getItem('token');
        if(token){
          next();
        }else{
          next({
            path: '/login',
            query: {
              from: to.path
            }
          })
        }
      } else {
        //  不需要登录
        next();
      }
    })
    const vm = new Vue({
      el: '#app',
      router
    })
  </script>
</body>

</html>